import { SaaSRoleListGetResultModel } from '../authority-mgmt/model/roleModel';
import {
  SaaSAuthListItem,
  SaaSRoleItem,
  SaaSSubAppItem,
  SaaSUserListItem,
  SaaSUserPageParams,
  SaaSUserParams,
  SaaSUserRoleParams,
  App,
  SaaSUserPasswordParams,
} from './model/userModel';
import { tenantHttp } from '/@/utils/http/axios';

enum Api {
  SaaSUserList = '/user/getList',
  SaaSUserRole = '/user/findById',
  AddSaaSUser = '/user/createUser',
  UpdateSaaSUser = '/user/updateUser',
  UpdateSaaSUserPassword = '/user/updatePassword',
  SaaSUserDelete = '/user/delete/',
  SaaSUserView = '/user/detail',
  SaaSUserAuth = '/user/listAuthorityApp',
  SaaSSubAppList = '/app/listAllSubApp',
  SaaSRoleByAppId = '/role/listRoleBySubAppId',
  SaaSAppList = 'tenant/subAppList',

  UserList = '/user/getList',
  userNameExist = '/user/userNameExist',
  phoneExist = '/user/phoneExist',
  AddOrUpdate = '/user/addOrUpdate',
  Delete = 'user/delete',
  OrgList = '/organization/getList',
  RoleList = '/role/getList',
}

// SaaS Landing 用户
export const getSaaSUserList = (params: SaaSUserPageParams) => {
  // 查询用户列表
  return tenantHttp.post<SaaSUserListItem>({ url: Api.SaaSUserList, params });
};

export const findSaaSUserRoleById = (params: SaaSUserRoleParams) => {
  // 查询指定用户的角色
  return tenantHttp.get<SaaSRoleListGetResultModel>({
    url: Api.SaaSUserRole,
    params,
  });
};

export const addSaaSUser = (params: SaaSUserParams) => {
  // 新增/编辑用户
  return tenantHttp.post<string>({ url: Api.AddSaaSUser, params });
};

export const updateSaaSUser = (params: SaaSUserParams) => {
  // 新增/编辑用户
  return tenantHttp.post<string>({ url: Api.UpdateSaaSUser, params });
};
export const updatePasswordSaaSUser = (params: SaaSUserPasswordParams) => {
  // 修改用户密码
  return tenantHttp.post<string>({ url: Api.UpdateSaaSUserPassword, params });
};
export const deleteSaaSUser = (params: string) => {
  // 删除用户
  return tenantHttp.delete<string>({ url: Api.SaaSUserDelete + params });
};

export const viewSaaSUser = async (params: { userId: string }) => {
  const res = await tenantHttp.get<SaaSUserListItem>({ url: Api.SaaSUserView, params });
  return res;
};

export const authSaaSUser = (params: { userId: string }) => {
  // 用户授权详情-用户ID
  return tenantHttp.get<SaaSAuthListItem[]>({ url: Api.SaaSUserAuth, params });
};
export const getSubAppList = () => {
  // 获取订阅应用列表
  return tenantHttp.get<SaaSSubAppItem[]>({ url: Api.SaaSSubAppList });
};

export const getRoleBySubAppId = (params: { subAppId: string }) => {
  return tenantHttp.get<SaaSRoleItem[]>({ url: Api.SaaSRoleByAppId, params });
};
export const getAppList = () => {
  // 通过订阅应用id获取角色列表
  return tenantHttp.get<App[]>({ url: Api.SaaSAppList });
};
